package com.tamakloe.guestbook;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@SuppressWarnings("serial")
public class MessageDownload extends HttpServlet{
	private static final Logger log = Logger.getLogger(MessageDownload.class.getName());
    private static final String VIEW = "messagedownload.jsp";
    private static final String ATTRIBUTE_FORM = "form";
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
    		throws ServletException, IOException {
	    // Show view.
	    req.getRequestDispatcher(VIEW).forward(req, resp);	    

    }
    
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
    		throws ServletException, IOException {
        // Prepare form bean.
    	MessageDownloadForm messageDownloadForm = new MessageDownloadForm();
        req.setAttribute(ATTRIBUTE_FORM, messageDownloadForm);

        // Process request and get result.
        messageDownloadForm.downloadMessages(req, resp);
        if (!messageDownloadForm.isSuccess()){        
	        // Post back.
	        try {
				req.getRequestDispatcher(VIEW).forward(req, resp);
			} catch (ServletException e) {
				StringWriter sw = new StringWriter();
				e.printStackTrace(new PrintWriter(sw));
				log.severe(sw.toString());
				log.warning(ATTRIBUTE_FORM + e.getMessage()); 
			}        
        } else {
    	    // Show view.
    	    //req.getRequestDispatcher(VIEW).forward(req, resp);
        }	    
    }
}
